package com.example.beikeyigou;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.beikeyigou.login.UserBean;
import com.example.beikeyigou.untils.BaseActivity;
import com.example.beikeyigou.untils.SaveUserUtils;
import com.example.beikeyigou.untils.URLUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class PersonCenterActivity extends BaseActivity {
    private UserBean userBean;
    private UserBean.DataBean dataBean;
    private UserBean.DataBean newDataBean;
    private CreateUserDialog createUserDialog;
    private Context context = this;

    TextView telTv,nickNameTv,nameTv,addressTv,saveTv;
    ImageView nickNameIv,nameIv,addressIv,backIv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_center);

        //获取传过来的数据
        /*Intent intent = getIntent();
        userBean = (UserBean) intent.getSerializableExtra("user");
        dataBean = userBean.getData();*/

        dataBean = SaveUserUtils.getUserInfo(this);
        newDataBean = new UserBean.DataBean(dataBean.getId(),dataBean.getPhone(),dataBean.getPassword(),dataBean.getName(),dataBean.getAddress()
        ,dataBean.getNickname(),dataBean.getQuestion(),dataBean.getAnswer()
        );



        initView();
        saveTv.setOnClickListener(onClickListener3);
        backIv.setOnClickListener(onClickListener4);


    }

    private void initView() {

        telTv = findViewById(R.id.center_tv_tel);
        nickNameTv = findViewById(R.id.center_tv_nickname);
        nameTv = findViewById(R.id.center_tv_name);
        addressTv = findViewById(R.id.center_tv_address);
        saveTv = findViewById(R.id.center_tv_save);

        nickNameIv = findViewById(R.id.center_iv_nickname);
        nameIv = findViewById(R.id.center_iv_name);
        addressIv = findViewById(R.id.center_iv_address);
        backIv = findViewById(R.id.center_iv_back);

        telTv.setText(dataBean.getPhone());
        nickNameTv.setText(dataBean.getNickname());
        nameTv.setText(dataBean.getName());
        addressTv.setText(dataBean.getAddress());


    }

    public void showEditDialog(View view) {
        createUserDialog = new CreateUserDialog(this,R.style.loading_dialog,onClickListener);
        createUserDialog.show();
    }

    public void showEditDialog1(View view) {
        createUserDialog = new CreateUserDialog(this,R.style.loading_dialog,onClickListener1);
        createUserDialog.show();
    }

    public void showEditDialog2(View view) {
        createUserDialog = new CreateUserDialog(this,R.style.loading_dialog,onClickListener2);
        createUserDialog.show();
    }

    //修改一
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String info = createUserDialog.text_info.getText().toString();

            createUserDialog.dismiss();
            nickNameTv.setText(info);
            }

        };

    //修改二
    private View.OnClickListener onClickListener1 = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String info = createUserDialog.text_info.getText().toString();

            createUserDialog.dismiss();
            nameTv.setText(info);


        }

    };

    //修改三
    private View.OnClickListener onClickListener2 = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String info = createUserDialog.text_info.getText().toString();

            createUserDialog.dismiss();
            addressTv.setText(info);

        }

    };

    //保存的监听
    private View.OnClickListener onClickListener3 = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
/*
            String phoneNum = (String) telTv.getText();
            String nickName = (String) nickNameTv.getText();
            String name = (String) nameTv.getText();
            String address = (String) addressTv.getText();
            boolean flag = judgeInput(phoneNum,nickName);

            //更新数据
            dataBean.setNickname(nickName);
            dataBean.setName(name);
            dataBean.setAddress(address);*/
            boolean flag = isChange();
            if (!flag) {
                Map<String,String> map = new HashMap<>();
                map.put("id",String.valueOf(dataBean.getId()));
                map.put("address", newDataBean.getAddress());
                map.put("nickname", newDataBean.getNickname());
                map.put("name", newDataBean.getName());
                postNetWork(URLUtils.updateinfo_url,map);
            }

        }

    };



    //返回监听
    private View.OnClickListener onClickListener4 = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (isChange()){
                finish();
            }
            else {
                showBackDialog();
            }

        }

    };

    private void showBackDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息");
        builder.setMessage("当前页面数据有改变,且未保存，是否仍要返回上级页面？");
        builder.setIcon(R.mipmap.icon);
        builder.setPositiveButton("取消",null);
        builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();//结束当前页面
            }
        });
        builder.create().show();
    }


    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        UserBean userBean = new Gson().fromJson(result,UserBean.class);
        //更新本地保存的信息，也是防止返回时弹出对话框
        dataBean = newDataBean;
        int code = userBean.getCode();
        switch (code){
            case 200://注册成功
                Toast.makeText(this,"更新成功！",Toast.LENGTH_SHORT).show();
                SaveUserUtils.saveUserToFile(this,newDataBean);
                break;
            default:
                Toast.makeText(this,"更新失败！",Toast.LENGTH_SHORT).show();
                break;

        }

    }

    //判断信息是否改变
    private boolean isChange(){
        String nickName = (String) nickNameTv.getText();
        String name = (String) nameTv.getText();
        String address = (String) addressTv.getText();
        newDataBean.setNickname(nickName);
        newDataBean.setName(name);
        newDataBean.setAddress(address);

        return newDataBean.equals(dataBean);
    }


    //判断输入的内容是否符合要求
    private boolean judgeInput(String nickName,String name,String address) {

        if (TextUtils.isEmpty(nickName) ||TextUtils.isEmpty(name)||TextUtils.isEmpty(address) ){

            Toast.makeText(this,"输入内容不可为空",Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;

    }




    }




